//
//  XS_OrderSubVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/21.
//

import UIKit

public enum XS_OrderSubVCType : Int {
    case all
    case payment
    case nopay
}

class XS_OrderSubVC: XS_BaseVC {

    lazy var dataModel = XS_OrderItemM()
    
    weak var nav : UINavigationController?
    
    public var type : XS_OrderSubVCType = .all
    
    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        collectionView.register(UINib.init(nibName: "XS_OrderItemCell", bundle: Bundle.main), forCellWithReuseIdentifier: "XS_OrderItemCell")
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(headerRefresh))
        collectionView.mj_footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(footerRefresh))
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        headerRefresh()
    }
    
    @objc func headerRefresh(){
        requestData(isFirst: true)
    }
    
    @objc func footerRefresh(){
        requestData(isFirst: false)
    }
    
    func requestData(isFirst : Bool){
        //        order/getUserOrderList 课程
        if isFirst {
            dataModel.current = 0
            dataModel.records.removeAll()
        }
        var state = ""
        switch type {
        case .payment:
            state = "payment"
            break
        case .nopay:
            state = "nopay"
            break
        default:
            //
            break
        }
        XS_NetWork.requestJson("order/getUserOrderList", parameters: ["pageNo" : dataModel.current+1, "pageSize" : dataModel.size, "state":state])
            .mapModel(type: XS_OrderItemM.self) // 返回 模型数组 和原始数据
            .subscribe(onNext: { [weak self] model, resp in
                guard let `self` = self, resp.success, let model = model else {return }
                model.records = self.dataModel.records + model.records
                if model.records.count > 0 {
                    XD_BlankPageView.hiddenBlankPageView(superView: self.collectionView)
                    self.dataModel = model
                    self.collectionView.reloadData()
                } else {
                    XD_BlankPageView.blankPage(superView: self.collectionView, imgName: "缺省页_暂无订单", detailStr: "暂无订单~", btnTitle: "去首页看看", bottomDetailStr: "") {
                        if let tbc = XS_NVC.shared.viewControllers.first as? XS_TBC {
                            tbc.selectedIndex = 0
                        }
                        self.nav?.popToRootViewController(animated: true)
                    }
                }
                self.collectionView.mj_header?.endRefreshing()
                if model.current >= model.pages { // 没有更多
                    self.collectionView.mj_footer?.endRefreshingWithNoMoreData()
                }else{
                    self.collectionView.mj_footer?.endRefreshing()
                }
            }).disposed(by: disposeBag)
    }

}

extension XS_OrderSubVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataModel.records.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XS_OrderItemCell", for: indexPath) as! XS_OrderItemCell
        if indexPath.item < dataModel.records.count {
            cell.model = dataModel.records[indexPath.item]
        }
        cell.delegate = self
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        var height = 160
        if indexPath.item < dataModel.records.count {
            let model = dataModel.records[indexPath.item]
            if model.state == "payment" || model.state == "cancel" {
                height = 121
            }
        }
        return CGSize.init(width: .screenWidth - 32, height: CGFloat(height))
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 9
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 12, left: 16, bottom: 12, right: 16)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
}

extension XS_OrderSubVC : XS_OrderItemCellDelegate{
    func orderItemCell(orderItemCell: XS_OrderItemCell, didClickCancelBtn: UIButton) {
        
    }
    
    func orderItemCell(orderItemCell: XS_OrderItemCell, didClickBuyBtn: UIButton) {
        
    }
    
    func orderItemCell(orderItemCell: XS_OrderItemCell, didClickLeftBtn: UIButton) {
        let vc = XS_KeChengVC()
        vc.id = orderItemCell.model.curriculumId
        nav?.pushViewController(vc, animated: true)
    }
    
    func orderItemCell(orderItemCell: XS_OrderItemCell, didClickRightBtn: UIButton) {
        let detailVC = XS_OrderDetailVC.init(nibName: "XS_OrderDetailVC", bundle: Bundle.main)
        detailVC.model = orderItemCell.model
        nav?.pushViewController(detailVC, animated: true)
    }
}
